home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / language.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  29KB  |  1,108 lines

  1. /* Multiple source language support for GDB.
  2.    Copyright 1991 Free Software Foundation, Inc.
  3.    Contributed by the Department of Computer Science at the State University
  4.    of New York at Buffalo.
  5.  
  6. This file is part of GDB.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. /* This file contains functions that return things that are specific
  23.    to languages.  Each function should examine current_language if necessary,
  24.    and return the appropriate result. */
  25.  
  26. /* FIXME:  Most of these would be better organized as macros which
  27.    return data out of a "language-specific" struct pointer that is set
  28.    whenever the working language changes.  That would be a lot faster.  */
  29.  
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <varargs.h>
  33.  
  34. #include "defs.h"
  35. #include "symtab.h"
  36. #include "value.h"
  37. #include "gdbcmd.h"
  38. #include "frame.h"
  39. #include "language.h"
  40. #include "expression.h"
  41. #include "target.h"
  42. #include "parser-defs.h"
  43.  
  44. /* Forward function declarations */
  45. static void set_type_range ();
  46.  
  47. /* Forward declaration */
  48. extern struct language_defn unknown_language_defn;
  49.  
  50. /* The current (default at startup) state of type and range checking.
  51.     (If the modes are set to "auto", though, these are changed based
  52.     on the default language at startup, and then again based on the
  53.     language of the first source file.  */
  54.  
  55. enum range_mode range_mode = range_mode_auto;
  56. enum range_check range_check = range_check_off;
  57. enum type_mode type_mode = type_mode_auto;
  58. enum type_check type_check = type_check_off;
  59.  
  60. /* The current language and language_mode (see language.h) */
  61.  
  62. struct language_defn *current_language = &unknown_language_defn;
  63. enum language_mode language_mode = language_mode_auto;
  64.  
  65. /* The list of supported languages.  The list itself is malloc'd.  */
  66.  
  67. static struct language_defn **languages;
  68. static unsigned languages_size;
  69. static unsigned languages_allocsize;
  70. #define    DEFAULT_ALLOCSIZE 3
  71.  
  72. /* The "set language/type/range" commands all put stuff in these
  73.    buffers.  This is to make them work as set/show commands.  The
  74.    user's string is copied here, then the set_* commands look at
  75.    them and update them to something that looks nice when it is
  76.    printed out. */
  77.  
  78. static char *language;
  79. static char *type;
  80. static char *range;
  81.  
  82. /* Warning issued when current_language and the language of the current
  83.    frame do not match. */
  84. char lang_frame_mismatch_warn[] =
  85.     "Warning: the current language does not match this frame.";
  86.  
  87. void set_lang_str();
  88. void set_type_str();
  89. void set_range_str();
  90.  
  91. /* This page contains the functions corresponding to GDB commands
  92.    and their helpers. */
  93.  
  94. /* Show command.  Display a warning if the language set
  95.    does not match the frame. */
  96. void
  97. show_language_command (ignore, from_tty)
  98.    char *ignore;
  99.    int from_tty;
  100. {
  101.    enum language flang;        /* The language of the current frame */
  102.  
  103.    flang = get_frame_language();
  104.    if (flang != language_unknown &&
  105.       language_mode == language_mode_manual &&
  106.       current_language->la_language != flang)
  107.       printf_filtered("%s\n",lang_frame_mismatch_warn);
  108. }
  109.  
  110. /* Set command.  Change the current working language. */
  111. void
  112. set_language_command (ignore, from_tty)
  113.    char *ignore;
  114.    int from_tty;
  115. {
  116.   int i;
  117.   enum language flang;
  118.   char *err_lang;
  119.  
  120.   /* FIXME -- do this from the list, with HELP.  */
  121.   if (!language || !language[0]) {
  122.     printf("The currently understood settings are:\n\n\
  123. local or auto    Automatic setting based on source file\n\
  124. c                Use the C language\n\
  125. modula-2         Use the Modula-2 language\n");
  126.     /* Restore the silly string. */
  127.     set_language(current_language->la_language);
  128.     return;
  129.   }
  130.  
  131.   /* Search the list of languages for a match.  */
  132.   for (i = 0; i < languages_size; i++) {
  133.     if (!strcmp (languages[i]->la_name, language)) {
  134.       /* Found it!  Go into manual mode, and use this language.  */
  135.       if (languages[i]->la_language == language_auto) {
  136.     /* Enter auto mode.  Set to the current frame's language, if known.  */
  137.     language_mode = language_mode_auto;
  138.       flang = get_frame_language();
  139.     if (flang!=language_unknown)
  140.       set_language(flang);
  141.     return;
  142.       } else {
  143.     /* Enter manual mode.  Set the specified language.  */
  144.     language_mode = language_mode_manual;
  145.     current_language = languages[i];
  146.     set_type_range ();
  147.     set_lang_str();
  148.     return;
  149.       }
  150.     }
  151.   }
  152.  
  153.   /* Reset the language (esp. the global string "language") to the 
  154.      correct values. */
  155.   err_lang=savestring(language,strlen(language));
  156.   make_cleanup (free, err_lang);    /* Free it after error */
  157.   set_language(current_language->la_language);
  158.   error ("Unknown language `%s'.",err_lang);
  159. }
  160.  
  161. /* Show command.  Display a warning if the type setting does
  162.    not match the current language. */
  163. void
  164. show_type_command(ignore, from_tty)
  165.    char *ignore;
  166.    int from_tty;
  167. {
  168.    if (type_check != current_language->la_type_check)
  169.       printf(
  170. "Warning: the current type check setting does not match the language.\n");
  171. }
  172.  
  173. /* Set command.  Change the setting for type checking. */
  174. void
  175. set_type_command(ignore, from_tty)
  176.    char *ignore;
  177.    int from_tty;
  178. {
  179.    if (!strcmp(type,"on"))
  180.    {
  181.       type_check = type_check_on;
  182.       type_mode = type_mode_manual;
  183.    }
  184.    else if (!strcmp(type,"warn"))
  185.    {
  186.       type_check = type_check_warn;
  187.       type_mode = type_mode_manual;
  188.    }
  189.    else if (!strcmp(type,"off"))
  190.    {
  191.       type_check = type_check_off;
  192.       type_mode = type_mode_manual;
  193.    }
  194.    else if (!strcmp(type,"auto"))
  195.    {
  196.       type_mode = type_mode_auto;
  197.       set_type_range();
  198.       /* Avoid hitting the set_type_str call below.  We
  199.          did it in set_type_range. */
  200.       return;
  201.    }
  202.    set_type_str();
  203.    show_type_command((char *)NULL, from_tty);
  204. }
  205.  
  206. /* Show command.  Display a warning if the range setting does
  207.    not match the current language. */
  208. void
  209. show_range_command(ignore, from_tty)
  210.    char *ignore;
  211.    int from_tty;
  212. {
  213.  
  214.    if (range_check != current_language->la_range_check)
  215.       printf(
  216. "Warning: the current range check setting does not match the language.\n");
  217. }
  218.  
  219. /* Set command.  Change the setting for range checking. */
  220. void
  221. set_range_command(ignore, from_tty)
  222.    char *ignore;
  223.    int from_tty;
  224. {
  225.    if (!strcmp(range,"on"))
  226.    {
  227.       range_check = range_check_on;
  228.       range_mode = range_mode_manual;
  229.    }
  230.    else if (!strcmp(range,"warn"))
  231.    {
  232.       range_check = range_check_warn;
  233.       range_mode = range_mode_manual;
  234.    }
  235.    else if (!strcmp(range,"off"))
  236.    {
  237.       range_check = range_check_off;
  238.       range_mode = range_mode_manual;
  239.    }
  240.    else if (!strcmp(range,"auto"))
  241.    {
  242.       range_mode = range_mode_auto;
  243.       set_type_range();
  244.       /* Avoid hitting the set_range_str call below.  We
  245.      did it in set_type_range. */
  246.       return;
  247.    }
  248.    set_range_str();
  249.    show_range_command((char *)0, from_tty);
  250. }
  251.  
  252. /* Set the status of range and type checking based on
  253.    the current modes and the current language.
  254.    If SHOW is non-zero, then print out the current language,
  255.    type and range checking status. */
  256. static void
  257. set_type_range()
  258. {
  259.  
  260.   if (range_mode == range_mode_auto)
  261.     range_check = current_language->la_range_check;
  262.  
  263.   if (type_mode == type_mode_auto)
  264.     type_check = current_language->la_type_check;
  265.  
  266.   set_type_str();
  267.   set_range_str();
  268. }
  269.  
  270. /* Set current language to (enum language) LANG.  */
  271.  
  272. void
  273. set_language(lang)
  274.    enum language lang;
  275. {
  276.   int i;
  277.  
  278.   for (i = 0; i < languages_size; i++) {
  279.     if (languages[i]->la_language == lang) {
  280.       current_language = languages[i];
  281.       set_type_range ();
  282.       set_lang_str();
  283.     }
  284.   }
  285. }
  286.  
  287. /* This page contains functions that update the global vars
  288.    language, type and range. */
  289. void
  290. set_lang_str()
  291. {
  292.    char *prefix = "";
  293.  
  294.    free (language);
  295.    if (language_mode == language_mode_auto)
  296.       prefix = "auto; currently ";
  297.  
  298.    language = concat(prefix, current_language->la_name, "");
  299. }
  300.  
  301. void
  302. set_type_str()
  303. {
  304.    char *tmp, *prefix = "";
  305.  
  306.    free (type);
  307.    if (type_mode==type_mode_auto)
  308.       prefix = "auto; currently ";
  309.  
  310.    switch(type_check)
  311.    {
  312.    case type_check_on:
  313.       tmp = "on";
  314.       break;
  315.    case type_check_off:
  316.       tmp = "off";
  317.       break;
  318.    case type_check_warn:
  319.       tmp = "warn";
  320.       break;
  321.       default:
  322.       error ("Unrecognized type check setting.");
  323.    }
  324.  
  325.    type = concat(prefix,tmp,"");
  326. }
  327.  
  328. void
  329. set_range_str()
  330. {
  331.    char *tmp, *pref = "";
  332.  
  333.    free (range);
  334.    if (range_mode==range_mode_auto)
  335.       pref = "auto; currently ";
  336.  
  337.    switch(range_check)
  338.    {
  339.    case range_check_on:
  340.       tmp = "on";
  341.       break;
  342.    case range_check_off:
  343.       tmp = "off";
  344.       break;
  345.    case range_check_warn:
  346.       tmp = "warn";
  347.       break;
  348.       default:
  349.       error ("Unrecognized range check setting.");
  350.    }
  351.  
  352.    range = concat(pref,tmp,"");
  353. }
  354.  
  355.  
  356. /* Print out the current language settings: language, range and
  357.    type checking. */
  358. void
  359. language_info ()
  360. {
  361.    printf("Current Language:  %s\n",language);
  362.    show_language_command((char *)0, 1);
  363.    printf("Type checking:     %s\n",type);
  364.    show_type_command((char *)0, 1);
  365.    printf("Range checking:    %s\n",range);
  366.    show_range_command((char *)0, 1);
  367. }
  368.  
  369. /* Return the result of a binary operation. */
  370. struct type *
  371. binop_result_type(v1,v2)
  372.    value v1,v2;
  373. {
  374.    int l1,l2,size,uns;
  375.  
  376.    l1 = TYPE_LENGTH(VALUE_TYPE(v1));
  377.    l2 = TYPE_LENGTH(VALUE_TYPE(v2));
  378.  
  379.    switch(current_language->la_language)
  380.    {
  381.    case language_c:
  382.       if (TYPE_CODE(VALUE_TYPE(v1))==TYPE_CODE_FLT)
  383.      return TYPE_CODE(VALUE_TYPE(v2)) == TYPE_CODE_FLT && l2 > l1 ?
  384.         VALUE_TYPE(v2) : VALUE_TYPE(v1);
  385.       else if (TYPE_CODE(VALUE_TYPE(v2))==TYPE_CODE_FLT)
  386.      return TYPE_CODE(VALUE_TYPE(v1)) == TYPE_CODE_FLT && l1 > l2 ?
  387.         VALUE_TYPE(v1) : VALUE_TYPE(v2);
  388.       else if (TYPE_UNSIGNED(VALUE_TYPE(v1)) && l1 > l2)
  389.      return VALUE_TYPE(v1);
  390.       else if (TYPE_UNSIGNED(VALUE_TYPE(v2)) && l2 > l1)
  391.      return VALUE_TYPE(v2);
  392.       else  /* Both are signed.  Result is the longer type */
  393.      return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2);
  394.       break;
  395.    case language_m2:
  396.       /* If we are doing type-checking, l1 should equal l2, so this is
  397.      not needed. */
  398.       return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2);
  399.       break;
  400.    }
  401.    abort();
  402.    return (struct type *)0;    /* For lint */
  403. }
  404.  
  405. /* This page contains functions that return format strings for
  406.    printf for printing out numbers in different formats */
  407.  
  408. /* Returns the appropriate printf format for hexadecimal
  409.    numbers. */
  410. char *
  411. local_hex_format_custom(pre)
  412.    char *pre;
  413. {
  414.    static char form[50];
  415.  
  416.    strcpy (form, current_language->la_hex_format_pre);
  417.    strcat (form, pre);
  418.    strcat (form, current_language->la_hex_format_suf);
  419.    return form;
  420. }
  421.  
  422. /* Converts a number to hexadecimal and stores it in a static
  423.    string.  Returns a pointer to this string. */
  424. char *
  425. local_hex_string (num)
  426.    int num;
  427. {
  428.    static char res[50];
  429.  
  430.    sprintf (res, current_language->la_hex_format, num);
  431.    return res;
  432. }
  433.  
  434. /* Converts a number to custom hexadecimal and stores it in a static
  435.    string.  Returns a pointer to this string. */
  436. char *
  437. local_hex_string_custom(num,pre)
  438.    int num;
  439.    char *pre;
  440. {
  441.    static char res[50];
  442.  
  443.    sprintf (res, local_hex_format_custom(pre), num);
  444.    return res;
  445. }
  446.  
  447. /* Returns the appropriate printf format for octal
  448.    numbers. */
  449. char *
  450. local_octal_format_custom(pre)
  451.    char *pre;
  452. {
  453.    static char form[50];
  454.  
  455.    strcpy (form, current_language->la_octal_format_pre);
  456.    strcat (form, pre);
  457.    strcat (form, current_language->la_octal_format_suf);
  458.    return form;
  459. }
  460.  
  461. /* This page contains functions that are used in type/range checking.
  462.    They all return zero if the type/range check fails.
  463.  
  464.    It is hoped that these will make extending GDB to parse different
  465.    languages a little easier.  These are primarily used in eval.c when
  466.    evaluating expressions and making sure that their types are correct.
  467.    Instead of having a mess of conjucted/disjuncted expressions in an "if",
  468.    the ideas of type can be wrapped up in the following functions.
  469.  
  470.    Note that some of them are not currently dependent upon which language
  471.    is currently being parsed.  For example, floats are the same in
  472.    C and Modula-2 (ie. the only floating point type has TYPE_CODE of
  473.    TYPE_CODE_FLT), while booleans are different. */
  474.  
  475. /* Returns non-zero if its argument is a simple type.  This is the same for
  476.    both Modula-2 and for C.  In the C case, TYPE_CODE_CHAR will never occur,
  477.    and thus will never cause the failure of the test. */
  478. int
  479. simple_type(type)
  480.     struct type *type;
  481. {
  482.   switch (TYPE_CODE (type)) {
  483.   case TYPE_CODE_INT:
  484.   case TYPE_CODE_CHAR:
  485.   case TYPE_CODE_ENUM:
  486.   case TYPE_CODE_FLT:
  487.   case TYPE_CODE_RANGE:
  488.   case TYPE_CODE_BOOL:
  489.     return 1;
  490.  
  491.   default:
  492.     return 0;
  493.   }
  494. }
  495.  
  496. /* Returns non-zero if its argument is of an ordered type. */
  497. int
  498. ordered_type (type)
  499.    struct type *type;
  500. {
  501.   switch (TYPE_CODE (type)) {
  502.   case TYPE_CODE_INT:
  503.   case TYPE_CODE_CHAR:
  504.   case TYPE_CODE_ENUM:
  505.   case TYPE_CODE_FLT:
  506.   case TYPE_CODE_RANGE:
  507.     return 1;
  508.  
  509.   default:
  510.     return 0;
  511.   }
  512. }
  513.  
  514. /* Returns non-zero if the two types are the same */
  515. int
  516. same_type (arg1, arg2)
  517.    struct type *arg1, *arg2;
  518. {
  519.    if (structured_type(arg1) ? !structured_type(arg2) : structured_type(arg2))
  520.       /* One is structured and one isn't */
  521.       return 0;
  522.    else if (structured_type(arg1) && structured_type(arg2))
  523.       return arg1 == arg2;
  524.    else if (numeric_type(arg1) && numeric_type(arg2))
  525.       return (TYPE_CODE(arg2) == TYPE_CODE(arg1)) &&
  526.      (TYPE_UNSIGNED(arg1) == TYPE_UNSIGNED(arg2))
  527.         ? 1 : 0;
  528.    else
  529.       return arg1==arg2;
  530. }
  531.  
  532. /* Returns non-zero if the type is integral */
  533. int
  534. integral_type (type)
  535.    struct type *type;
  536. {
  537.    switch(current_language->la_language)
  538.    {
  539.    case language_c:
  540.       return (TYPE_CODE(type) != TYPE_CODE_INT) &&
  541.      (TYPE_CODE(type) != TYPE_CODE_ENUM) ? 0 : 1;
  542.    case language_m2:
  543.       return TYPE_CODE(type) != TYPE_CODE_INT ? 0 : 1;
  544.    default:
  545.       error ("Language not supported.");
  546.    }
  547. }
  548.  
  549. /* Returns non-zero if the value is numeric */
  550. int
  551. numeric_type (type)
  552.    struct type *type;
  553. {
  554.   switch (TYPE_CODE (type)) {
  555.   case TYPE_CODE_INT:
  556.   case TYPE_CODE_FLT:
  557.     return 1;
  558.  
  559.   default:
  560.     return 0;
  561.   }
  562. }
  563.  
  564. /* Returns non-zero if the value is a character type */
  565. int
  566. character_type (type)
  567.    struct type *type;
  568. {
  569.    switch(current_language->la_language)
  570.    {
  571.    case language_m2:
  572.       return TYPE_CODE(type) != TYPE_CODE_CHAR ? 0 : 1;
  573.  
  574.    case language_c:
  575.       return (TYPE_CODE(type) == TYPE_CODE_INT) &&
  576.      TYPE_LENGTH(type) == sizeof(char)
  577.      ? 1 : 0;
  578.    }
  579. }
  580.  
  581. /* Returns non-zero if the value is a boolean type */
  582. int
  583. boolean_type (type)
  584.    struct type *type;
  585. {
  586.    switch(current_language->la_language)
  587.    {
  588.    case language_m2:
  589.       return TYPE_CODE(type) != TYPE_CODE_BOOL ? 0 : 1;
  590.  
  591.    case language_c:
  592.       return TYPE_CODE(type) != TYPE_CODE_INT ? 0 : 1;
  593.    }
  594. }
  595.  
  596. /* Returns non-zero if the value is a floating-point type */
  597. int
  598. float_type (type)
  599.    struct type *type;
  600. {
  601.    return TYPE_CODE(type) == TYPE_CODE_FLT;
  602. }
  603.  
  604. /* Returns non-zero if the value is a pointer type */
  605. int
  606. pointer_type(type)
  607.    struct type *type;
  608. {
  609.    return TYPE_CODE(type) == TYPE_CODE_PTR ||
  610.       TYPE_CODE(type) == TYPE_CODE_REF;
  611. }
  612.  
  613. /* Returns non-zero if the value is a structured type */
  614. int
  615. structured_type(type)
  616.    struct type *type;
  617. {
  618.    switch(current_language->la_language)
  619.    {
  620.    case language_c:
  621.       return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
  622.      (TYPE_CODE(type) == TYPE_CODE_UNION) ||
  623.         (TYPE_CODE(type) == TYPE_CODE_ARRAY);
  624.    case language_m2:
  625.       return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
  626.      (TYPE_CODE(type) == TYPE_CODE_SET) ||
  627.         (TYPE_CODE(type) == TYPE_CODE_ARRAY);
  628.    }
  629. }
  630.  
  631. /* This page contains functions that return info about
  632.    (struct value) values used in GDB. */
  633.  
  634. /* Returns non-zero if the value VAL represents a true value. */
  635. int
  636. value_true(val)
  637.      value val;
  638. {
  639.   int len, i;
  640.   struct type *type;
  641.   LONGEST v;
  642.  
  643.   switch (current_language->la_language) {
  644.  
  645.   case language_c:
  646.     return !value_zerop (val);
  647.  
  648.   case language_m2:
  649.     type = VALUE_TYPE(val);
  650.     if (TYPE_CODE (type) != TYPE_CODE_BOOL)
  651.       return 0;        /* Not a BOOLEAN at all */
  652.     /* Search the fields for one that matches the current value. */
  653.     len = TYPE_NFIELDS (type);
  654.     v = value_as_long (val);
  655.     for (i = 0; i < len; i++)
  656.       {
  657.     QUIT;
  658.     if (v == TYPE_FIELD_BITPOS (type, i))
  659.       break;
  660.       }
  661.     if (i >= len)
  662.       return 0;        /* Not a valid BOOLEAN value */
  663.     if (!strcmp ("TRUE", TYPE_FIELD_NAME(VALUE_TYPE(val), i)))
  664.       return 1;        /* BOOLEAN with value TRUE */
  665.     else
  666.       return 0;        /* BOOLEAN with value FALSE */
  667.     break;
  668.  
  669.   default:
  670.     error ("Language not supported.");
  671.   }
  672. }
  673.  
  674. /* Returns non-zero if the operator OP is defined on
  675.    the values ARG1 and ARG2. */
  676. void
  677. binop_type_check(arg1,arg2,op)
  678.    value arg1,arg2;
  679.    int op;
  680. {
  681.    struct type *t1, *t2;
  682.  
  683.    /* If we're not checking types, always return success. */
  684.    if (!STRICT_TYPE)
  685.       return;
  686.  
  687.    t1=VALUE_TYPE(arg1);
  688.    if (arg2!=(value)NULL)
  689.       t2=VALUE_TYPE(arg2);
  690.    else
  691.       t2=NULL;
  692.  
  693.    switch(op)
  694.    {
  695.    case BINOP_ADD:
  696.    case BINOP_SUB:
  697.       if ((numeric_type(t1) && pointer_type(t2)) ||
  698.      (pointer_type(t1) && numeric_type(t2)))
  699.       {
  700.      printf("warning:  combining pointer and integer.\n");
  701.      break;
  702.       }
  703.    case BINOP_MUL:
  704.    case BINOP_LSH:
  705.    case BINOP_RSH:
  706.       if (!numeric_type(t1) || !numeric_type(t2))
  707.      type_op_error ("Arguments to %s must be numbers.",op);
  708.       else if (!same_type(t1,t2))
  709.      type_op_error ("Arguments to %s must be of the same type.",op);
  710.       break;
  711.  
  712.    case BINOP_AND:
  713.    case BINOP_OR:
  714.       if (!boolean_type(t1) || !boolean_type(t2))
  715.      type_op_error ("Arguments to %s must be of boolean type.",op);
  716.       break;
  717.  
  718.    case BINOP_EQUAL:
  719.       if ((pointer_type(t1) && !(pointer_type(t2) || integral_type(t2))) ||
  720.      (pointer_type(t2) && !(pointer_type(t1) || integral_type(t1))))
  721.      type_op_error ("A pointer can only be compared to an integer or pointer.",op);
  722.       else if ((pointer_type(t1) && integral_type(t2)) ||
  723.      (integral_type(t1) && pointer_type(t2)))
  724.       {
  725.      printf("warning:  combining integer and pointer.\n");
  726.      break;
  727.       }
  728.       else if (!simple_type(t1) || !simple_type(t2))
  729.      type_op_error ("Arguments to %s must be of simple type.",op);
  730.       else if (!same_type(t1,t2))
  731.      type_op_error ("Arguments to %s must be of the same type.",op);
  732.       break;
  733.  
  734.    case BINOP_REM:
  735.       if (!integral_type(t1) || !integral_type(t2))
  736.      type_op_error ("Arguments to %s must be of integral type.",op);
  737.       break;
  738.  
  739.    case BINOP_LESS:
  740.    case BINOP_GTR:
  741.    case BINOP_LEQ:
  742.    case BINOP_GEQ:
  743.       if (!ordered_type(t1) || !ordered_type(t2))
  744.      type_op_error ("Arguments to %s must be of ordered type.",op);
  745.       else if (!same_type(t1,t2))
  746.      type_op_error ("Arguments to %s must be of the same type.",op);
  747.       break;
  748.  
  749.    case BINOP_ASSIGN:
  750.       if (pointer_type(t1) && !integral_type(t2))
  751.      type_op_error ("A pointer can only be assigned an integer.",op);
  752.       else if (pointer_type(t1) && integral_type(t2))
  753.       {
  754.      printf("warning:  combining integer and pointer.");
  755.      break;
  756.       }
  757.       else if (!simple_type(t1) || !simple_type(t2))
  758.      type_op_error ("Arguments to %s must be of simple type.",op);
  759.       else if (!same_type(t1,t2))
  760.      type_op_error ("Arguments to %s must be of the same type.",op);
  761.       break;
  762.  
  763.    /* Unary checks -- arg2 is null */
  764.  
  765.    case UNOP_ZEROP:
  766.       if (!boolean_type(t1))
  767.      type_op_error ("Argument to %s must be of boolean type.",op);
  768.       break;
  769.  
  770.    case UNOP_PLUS:
  771.    case UNOP_NEG:
  772.       if (!numeric_type(t1))
  773.      type_op_error ("Argument to %s must be of numeric type.",op);
  774.       break;
  775.  
  776.    case UNOP_IND:
  777.       if (integral_type(t1))
  778.       {
  779.      printf("warning:  combining pointer and integer.\n");
  780.      break;
  781.       }
  782.       else if (!pointer_type(t1))
  783.      type_op_error ("Argument to %s must be a pointer.",op);
  784.       break;
  785.  
  786.    case UNOP_PREINCREMENT:
  787.    case UNOP_POSTINCREMENT:
  788.    case UNOP_PREDECREMENT:
  789.    case UNOP_POSTDECREMENT:
  790.       if (!ordered_type(t1))
  791.      type_op_error ("Argument to %s must be of an ordered type.",op);
  792.       break;
  793.  
  794.    default:
  795.       /* Ok.  The following operators have different meanings in
  796.      different languages. */
  797.       switch(current_language->la_language)
  798.       {
  799. #ifdef _LANG_c
  800.       case language_c:
  801.      switch(op)
  802.      {
  803.      case BINOP_DIV:
  804.         if (!numeric_type(t1) || !numeric_type(t2))
  805.            type_op_error ("Arguments to %s must be numbers.",op);
  806.         break;
  807.      }
  808.      break;
  809. #endif
  810.  
  811. #ifdef _LANG_m2
  812.       case language_m2:
  813.      switch(op)
  814.      {
  815.      case BINOP_DIV:
  816.         if (!float_type(t1) || !float_type(t2))
  817.            type_op_error ("Arguments to %s must be floating point numbers.",op);
  818.         break;
  819.      case BINOP_INTDIV:
  820.         if (!integral_type(t1) || !integral_type(t2))
  821.            type_op_error ("Arguments to %s must be of integral type.",op);
  822.         break;
  823.      }
  824. #endif
  825.       }
  826.    }
  827. }
  828.  
  829. /* This page contains functions for the printing out of
  830.    error messages that occur during type- and range-
  831.    checking. */
  832.  
  833. /* Prints the format string FMT with the operator as a string
  834.    corresponding to the opcode OP.  If FATAL is non-zero, then
  835.    this is an error and error () is called.  Otherwise, it is
  836.    a warning and printf() is called. */
  837. void
  838. op_error (fmt,op,fatal)
  839.    char *fmt;
  840.    enum exp_opcode op;
  841.    int fatal;
  842. {
  843.    if (fatal)
  844.       error (fmt,op_string(op));
  845.    else
  846.    {
  847.       printf("warning:  ");
  848.       printf(fmt,op_string(op));
  849.       printf("\n");
  850.    }
  851. }
  852.  
  853. /* These are called when a language fails a type- or range-check.
  854.    The first argument should be a printf()-style format string, and
  855.    the rest of the arguments should be its arguments.  If
  856.    [type|range]_check is [type|range]_check_on, then return_to_top_level()
  857.    is called in the style of error ().  Otherwise, the message is prefixed
  858.    by "warning:  " and we do not return to the top level. */
  859. void
  860. type_error (va_alist)
  861.    va_dcl
  862. {
  863.    va_list args;
  864.    char *string;
  865.  
  866.    if (type_check==type_check_warn)
  867.       fprintf(stderr,"warning:  ");
  868.    else
  869.       target_terminal_ours();
  870.  
  871.    va_start (args);
  872.    string = va_arg (args, char *);
  873.    vfprintf (stderr, string, args);
  874.    fprintf (stderr, "\n");
  875.    va_end (args);
  876.    if (type_check==type_check_on)
  877.       return_to_top_level();
  878. }
  879.  
  880. void
  881. range_error (va_alist)
  882.    va_dcl
  883. {
  884.    va_list args;
  885.    char *string;
  886.  
  887.    if (range_check==range_check_warn)
  888.       fprintf(stderr,"warning:  ");
  889.    else
  890.       target_terminal_ours();
  891.  
  892.    va_start (args);
  893.    string = va_arg (args, char *);
  894.    vfprintf (stderr, string, args);
  895.    fprintf (stderr, "\n");
  896.    va_end (args);
  897.    if (range_check==range_check_on)
  898.       return_to_top_level();
  899. }
  900.  
  901.  
  902. /* This page contains miscellaneous functions */
  903.  
  904. /* Return the language as a string */
  905. char *
  906. language_str(lang)
  907.    enum language lang;
  908. {
  909.   int i;
  910.  
  911.   for (i = 0; i < languages_size; i++) {
  912.     if (languages[i]->la_language == lang) {
  913.       return languages[i]->la_name;
  914.     }
  915.   }
  916.   return "Unknown";
  917. }
  918.  
  919. struct cmd_list_element *setchecklist = NULL;
  920. struct cmd_list_element *showchecklist = NULL;
  921.  
  922. static void
  923. set_check (ignore, from_tty)
  924.    char *ignore;
  925.    int from_tty;
  926. {
  927.    printf(
  928. "\"set check\" must be followed by the name of a check subcommand.\n");
  929.    help_list(setchecklist, "set check ", -1, stdout);
  930. }
  931.  
  932. static void
  933. show_check (arg, from_tty)
  934.    char *arg;
  935.    int from_tty;
  936. {
  937.    cmd_show_list(showchecklist, from_tty, "");
  938. }
  939.  
  940. /* Add a language to the set of known languages.  */
  941.  
  942. void
  943. add_language (lang)
  944.      struct language_defn *lang;
  945. {
  946.   if (lang->la_magic != LANG_MAGIC)
  947.     {
  948.       fprintf(stderr, "Magic number of %s language struct wrong\n",
  949.     lang->la_name);
  950.       abort();
  951.     }
  952.  
  953.   if (!languages)
  954.     {
  955.       languages_allocsize = DEFAULT_ALLOCSIZE;
  956.       languages = (struct language_defn **) xmalloc
  957.     (languages_allocsize * sizeof (*languages));
  958.     }
  959.   if (languages_size >= languages_allocsize)
  960.     {
  961.       languages_allocsize *= 2;
  962.       languages = (struct language_defn **) xrealloc (languages,
  963.     languages_allocsize * sizeof (*languages));
  964.     }
  965.   languages[languages_size++] = lang;
  966.  
  967. #if FIXME
  968.   if (targetlist == NULL)
  969.     add_prefix_cmd ("target", class_run, target_command,
  970.             "Connect to a target machine or process.\n\
  971. The first argument is the type or protocol of the target machine.\n\
  972. Remaining arguments are interpreted by the target protocol.  For more\n\
  973. information on the arguments for a particular protocol, type\n\
  974. `help target ' followed by the protocol name.",
  975.             &targetlist, "target ", 0, &cmdlist);
  976.   add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
  977. #endif FIXME
  978. }
  979.  
  980. /* Define the language that is no language.  */
  981.  
  982. int
  983. unk_lang_parser ()
  984. {
  985.   return 1;
  986. }
  987.  
  988. void
  989. unk_lang_error ()
  990. {
  991.   error ("Attempted to parse an expression with unknown language");
  992. }
  993.  
  994. static struct type ** const (unknown_builtin_types[]) = { 0 };
  995. static const struct op_print unk_op_print_tab[] = { 0 };
  996.  
  997. const struct language_defn unknown_language_defn = {
  998.   "unknown",
  999.   language_unknown,
  1000.   &unknown_builtin_types[0],
  1001.   range_check_off,
  1002.   type_check_off,
  1003.   unk_lang_parser,
  1004.   unk_lang_error,
  1005.   &builtin_type_error,        /* longest signed   integral type */
  1006.   &builtin_type_error,        /* longest unsigned integral type */
  1007.   &builtin_type_error,        /* longest floating point type */
  1008.   "0x%x", "0x%", "x",        /* Hex   format, prefix, suffix */
  1009.   "0%o",  "0%",  "o",        /* Octal format, prefix, suffix */
  1010.   unk_op_print_tab,        /* expression operators for printing */
  1011.   LANG_MAGIC
  1012. };
  1013.  
  1014. /* These two structs define fake entries for the "local" and "auto" options. */
  1015. const struct language_defn auto_language_defn = {
  1016.   "auto",
  1017.   language_auto,
  1018.   &unknown_builtin_types[0],
  1019.   range_check_off,
  1020.   type_check_off,
  1021.   unk_lang_parser,
  1022.   unk_lang_error,
  1023.   &builtin_type_error,        /* longest signed   integral type */
  1024.   &builtin_type_error,        /* longest unsigned integral type */
  1025.   &builtin_type_error,        /* longest floating point type */
  1026.   "0x%x", "0x%", "x",        /* Hex   format, prefix, suffix */
  1027.   "0%o",  "0%",  "o",        /* Octal format, prefix, suffix */
  1028.   unk_op_print_tab,        /* expression operators for printing */
  1029.   LANG_MAGIC
  1030. };
  1031.  
  1032. const struct language_defn local_language_defn = {
  1033.   "local",
  1034.   language_auto,
  1035.   &unknown_builtin_types[0],
  1036.   range_check_off,
  1037.   type_check_off,
  1038.   unk_lang_parser,
  1039.   unk_lang_error,
  1040.   &builtin_type_error,        /* longest signed   integral type */
  1041.   &builtin_type_error,        /* longest unsigned integral type */
  1042.   &builtin_type_error,        /* longest floating point type */
  1043.   "0x%x", "0x%", "x",        /* Hex   format, prefix, suffix */
  1044.   "0%o",  "0%",  "o",        /* Octal format, prefix, suffix */
  1045.   unk_op_print_tab,        /* expression operators for printing */
  1046.   LANG_MAGIC
  1047. };
  1048.  
  1049. /* Initialize the language routines */
  1050.  
  1051. void
  1052. _initialize_language()
  1053. {
  1054.    struct cmd_list_element *set, *show;
  1055.  
  1056.    /* GDB commands for language specific stuff */
  1057.  
  1058.    set = add_set_cmd ("language", class_support, var_string_noescape,
  1059.               (char *)&language,
  1060.               "Set the current source language.",
  1061.               &setlist);
  1062.    show = add_show_from_set (set, &showlist);
  1063.    set->function = set_language_command;
  1064.    show->function = show_language_command;
  1065.  
  1066.    add_prefix_cmd ("check", no_class, set_check,
  1067.            "Set the status of the type/range checker",
  1068.            &setchecklist, "set check ", 0, &setlist);
  1069.    add_alias_cmd ("c", "check", no_class, 1, &setlist);
  1070.    add_alias_cmd ("ch", "check", no_class, 1, &setlist);
  1071.  
  1072.    add_prefix_cmd ("check", no_class, show_check,
  1073.            "Show the status of the type/range checker",
  1074.            &showchecklist, "show check ", 0, &showlist);
  1075.    add_alias_cmd ("c", "check", no_class, 1, &showlist);
  1076.    add_alias_cmd ("ch", "check", no_class, 1, &showlist);
  1077.  
  1078.    set = add_set_cmd ("type", class_support, var_string_noescape,
  1079.               (char *)&type,
  1080.               "Set type checking.  (on/warn/off/auto)",
  1081.               &setchecklist);
  1082.    show = add_show_from_set (set, &showchecklist);
  1083.    set->function = set_type_command;
  1084.    show->function = show_type_command;
  1085.  
  1086.    set = add_set_cmd ("range", class_support, var_string_noescape,
  1087.               (char *)&range,
  1088.               "Set range checking.  (on/warn/off/auto)",
  1089.               &setchecklist);
  1090.    show = add_show_from_set (set, &showchecklist);
  1091.    set->function = set_range_command;
  1092.    show->function = show_range_command;
  1093.  
  1094.    add_language (&unknown_language_defn);
  1095.    add_language (&local_language_defn);
  1096.    add_language (&auto_language_defn);
  1097.  
  1098.    language = savestring ("auto",strlen("auto"));
  1099.    range = savestring ("auto",strlen("auto"));
  1100.    type = savestring ("auto",strlen("auto"));
  1101.  
  1102.    /* Have the above take effect */
  1103.  
  1104.    set_language_command (language, 0);
  1105.    set_type_command (NULL, 0);
  1106.    set_range_command (NULL, 0);
  1107. }
  1108.